package com.hyb.i18n.config;

import java.util.Locale;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

@Configuration
public class WebConfigure implements WebMvcConfigurer{

	@Override
	 public void addInterceptors(InterceptorRegistry registry) {
		//添加国际化拦截器
		registry.addInterceptor(localeChangeInterceptor());
	}
	
	/**
     * 国际化切换拦截器
     * 
     * @return 国际化切换拦截器
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        //自定义拦截参数名称
        interceptor.setParamName("lang");
        return interceptor;
    }

    /**
     * 国际化处理器
     * 
     * @return 国际化处理器
     */
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver slr = new SessionLocaleResolver();
        //设置默认区域
        slr.setDefaultLocale(Locale.CHINA);
        return slr;
    }
}
